package com.leoman.controller.company;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.CompanyUser;
import com.leoman.entity.CompanyUserGain;
import com.leoman.service.CompanyUserGainService;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 财务记录管理
 * Created by 涂奕恒 on 2016/12/22.
 */
@Controller
@RequestMapping(value = "company/gain")
public class CompanyGainController extends CommonController {

    @Autowired
    private CompanyUserGainService companyUserGainService;

    @RequestMapping("index")
    public String index(HttpServletRequest request, ModelMap model) {
        CompanyUser companyUser = (CompanyUser) request.getSession().getAttribute(Constant.CURRENT_ADMIN_USER);

        if (null == companyUser) {
            return "redirect:/company/login";
        }

        model.addAttribute("companyUserId", companyUser.getId());
        return "company/财务记录列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer companyUserId,
                     Integer status,
                     String startTime,
                     String endTime,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Page<CompanyUserGain> page = companyUserGainService.pageByParams(companyUserId, status, startTime, endTime, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }
}
